package com.joplus.housekeeper.ui.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.utils.T;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * author： ccm
 * time： 2017/4/10.
 * description：
 */

public class ConservePicDialog extends PopupWindow {
    Context mContext;
    Activity activity;

    public ConservePicDialog(Context context) {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mMenuView = inflater.inflate(R.layout.dialog_conservepic, null);
        TextView tv_conserve = (TextView) mMenuView.findViewById(R.id.tv_conserve);
        TextView tv_dismiss = (TextView) mMenuView.findViewById(R.id.tv_dismiss);

        //设置按钮监听

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

        tv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tv_conserve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pic != null) {
                    saveImageToGallery(pic);
                }
            }
        });
    }

    public void show(final Activity activity) {
        this.activity = activity;
        backgroundAlpha(activity, 0.5f);
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

                backgroundAlpha(activity, 1);

            }
        });

    }

    Bitmap pic;

    public void setBitmap(Bitmap bitmap) {
        pic = bitmap;
    }

    public static void backgroundAlpha(Activity a, float bgAlpha) {
        WindowManager.LayoutParams lp = a.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            a.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            a.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        a.getWindow().setAttributes(lp);
    }

    public void saveImageToGallery(Bitmap bmp) {
        if (bmp == null) {
            T.showShort(mContext, "图片出错...");
            return;
        }

        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "jo_pic");
        if (!appDir.exists()) {
            appDir.mkdir();
        }

        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            T.showShort(mContext, "文件未发现");
            e.printStackTrace();
        } catch (IOException e) {
            T.showShort(mContext, "保存出错了...");
            e.printStackTrace();
        }

        // 最后通知图库更新
        try {
            MediaStore.Images.Media.insertImage(mContext.getContentResolver(), file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        mContext.sendBroadcast(intent);

        Uri localUri = Uri.fromFile(file);
        Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
        mContext.sendBroadcast(localIntent);

        T.showShort(mContext, "保存成功了...");

        dismiss();
    }


}
